package gui.gui2d.highLevelPanels.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import utility.Color;
import utility.Point;
import world.Piece;

public class ManageEnemysKings {
	private Lock lockblack= new ReentrantLock();
	private Lock lockWhite= new ReentrantLock();
	private Condition conditioPossibleEnemyBlack= lockblack.newCondition();
	private Condition conditioPossibleEnemyWhite= lockWhite.newCondition();
	
	private ArrayList<Piece> possibleEnemyOfBlackKing= new ArrayList<Piece>();
	private ArrayList<Piece> possibleEnemyOfWhiteKing= new ArrayList<Piece>();
	
	Point whiteKingPosition;
	Point blackKingPosition;
	
	public void getPossibleEnemyOfBlackKing(Piece[][] matrix , Point from , Point whiteKingPosition, Point blackKingPosition ){
		this.whiteKingPosition=whiteKingPosition;
		this.blackKingPosition=blackKingPosition;
		Piece attackPiece = matrix[from.getX()][from.getY()];
		attackPiece.setPosition(from);
		Color attackColor = attackPiece.getColor();
		if(attackColor.equals(Color.BLACK)){
			if(attackPiece.canMove(from, whiteKingPosition)){
				lockWhite.lock();
				possibleEnemyOfWhiteKing.add(attackPiece);
				conditioPossibleEnemyWhite.signalAll();
				lockWhite.unlock();
			}
		}else{
			if(attackPiece.canMove(from, blackKingPosition)){
				lockblack.lock();
				possibleEnemyOfBlackKing.add(attackPiece);
				conditioPossibleEnemyBlack.signalAll();
				lockblack.unlock();
			}
		}
		System.out.println(possibleEnemyOfBlackKing);
		System.out.println(possibleEnemyOfWhiteKing);
	}
	
	public void clearWhite(Piece[][] matrix) {
		lockWhite.lock();
		while(possibleEnemyOfWhiteKing.isEmpty() ){
			try {
				conditioPossibleEnemyWhite.await();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		Iterator<Piece> it = possibleEnemyOfWhiteKing.iterator();
		while(it.hasNext()){
			Piece p = it.next();
			if(!p.equals(matrix[p.getPosition().getX()][p.getPosition().getY()])){
				it.remove();
			}
			if(!p.canMove(p.getPosition(), whiteKingPosition)){
				it.remove();
			}
		}
		lockWhite.unlock();
	}
	
	public void clearBlack(Piece[][] matrix) {
		lockblack.lock();
		while(possibleEnemyOfBlackKing.isEmpty() ){
			try {
				conditioPossibleEnemyBlack.await();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		Iterator<Piece> it1 = possibleEnemyOfBlackKing.iterator();
		while(it1.hasNext()){
			Piece p = it1.next();
			if(!p.equals(matrix[p.getPosition().getX()][p.getPosition().getY()])){
				it1.remove();
			}
			if(!p.canMove(p.getPosition(), blackKingPosition)){
				it1.remove();
			}
			
		}
		lockblack.unlock();
	}
	
	boolean isVoidEnemyOfBlackKing(){
		return possibleEnemyOfBlackKing.isEmpty();
	}
	
	boolean isVoidEnemyOfWhiteKing(){
		return possibleEnemyOfWhiteKing.isEmpty();
	}
	
	
}
